package com.xw.service.xw.cycf;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.xw.dao.DaoSupport;
import com.xw.util.PageData;

/**
 * 类名称: CycfService
 * 类描述: 常用处方业务类
 * @author : gce
 * @date   : 2017-12-1
 */
@Service("cycfService")
public class CycfService {
	
	@Resource(name = "daoSupport")
	private DaoSupport dao;
	
	/**
	 * 判断常用处方名称是否存在
	 * @param cycfName
	 * @throws Exception
	 * @return: boolean
	 * @author: gce
	 * @date  : 2018-1-6
	 */
	public boolean hasByCycfName(String cycfName) throws Exception{
		List<PageData> list = (List<PageData>)dao.findForList("CycfMapper.findByCycfName", cycfName);
		return list.size()>0?true:false;
	}
	
	/**
	 * 根据医师ID，查询常用处方
	 * @return: List<PageData>
	 * @author: gce
	 * @throws Exception 
	 * @date  : 2017-12-1
	 */
	public List<PageData> list(PageData pd) throws Exception {
		return (List<PageData>)dao.findForList("CycfMapper.list", pd);
	}
	
	/**
	 * 根据常用处方ID，查询常用处方药品
	 * @return: List<PageData>
	 * @author: gce
	 * @throws Exception 
	 * @date  : 2017-12-1
	 */
	public List<PageData> cycfyplist(PageData pd) throws Exception {
		return (List<PageData>)dao.findForList("CycfMapper.cycfyplist", pd);
	}
	
	/**
	 * 添加常用处方
	 * @param pd （医师ID:USER_ID、常用处方名称cycfName、药品id数组ypIdArray、药品用量数组numberArray）
	 * @return: int
	 * @author: gce
	 * @throws Exception 
	 * @date  : 2018-1-5
	 */
	public int add(PageData pd,String[] ypIdArray,String[] numberArray) throws Exception{
		//添加常用处方
		int result = (int) dao.save("CycfMapper.saveCycf", pd);
		//添加常用处方药品
		if(result>0){
			
			for (int i = 0; i < ypIdArray.length; i++) {
				PageData pd2 = new PageData();
				pd2.put("cycfId",String.valueOf(pd.get("id")) );
				pd2.put("ypId", ypIdArray[i]);
				pd2.put("number",numberArray[i] );
				dao.save("CycfMapper.saveCycfyp", pd2);
			}
			return result;
		}else{
			return 0 ;
		}
	}
	
	/**
	 * 删除常用处方
	 * @param pd
	 * @throws Exception
	 * @return: int
	 * @author: gce
	 * @date  : 2018-1-6
	 */
	public int delCycf(PageData pd) throws Exception{
		return  (int)dao.delete("CycfMapper.delCycf", pd);
	}

}
